﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.eLogisticsQuery
{
    [Navigation("报表查询", "应收款查询")]
    public partial class AccountReceiveQueryPage : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {

                txtConfirmInvoiceDateStart.Text =
 DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtConfirmInvoiceDateEnd.Text =
    DateTime.Now.ToString("yyyy-MM-dd");
                ddlCompany.InsertEmptyItem(ListEmptyType.Select);
                this.ddlCustomer.InsertEmptyItem(ListEmptyType.Select);

                if (!this.IsSuperRoles())
                {
                    ddlCompany.SelectedValue = CurrentApplicationContext.CompanyId.ToString();
                    ddlCompany.Enabled = false;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ObjectDataSourceCustomers.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Session["AccountReceiveInvoice"] = GetSearchCriteria();
            HttpContext.Current.Session["AccountReceiveInvoceReceipts"] = GetAccountInvoiceReceiptsSearchCriteria();
            this.ScriptStartup("search();");

        }
        protected RevenueMachineAccountInvoiceReceiptsSearchCriteria GetAccountInvoiceReceiptsSearchCriteria()
        {
            var criteria = new RevenueMachineAccountInvoiceReceiptsSearchCriteria
                               {

                               };
            if (!MiscUtilities.IsNullOrEmpty(ddlAccountsReceiptType.SelectedValue))
            {
                criteria.AccountReceiptType = ddlAccountsReceiptType.SelectedValue.ToGuid();
            }
            return criteria;
        }

        protected RevenueMachineAccountInvoiceSearchCriteria GetSearchCriteria()
        {
            var criteria = new RevenueMachineAccountInvoiceSearchCriteria
            {

            };
            if (!MiscUtilities.IsNullOrEmpty(ddlCompany.SelectedValue))
            {
                criteria.AreaId = ddlCompany.SelectedValue.ToGuid();
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlCurrencyType.SelectedValue.ToGuid()))
            {
                criteria.CurrencyType = ddlCurrencyType.SelectedValue.ToGuid();
            }

            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            DateTime? dateStart = null, dateEnd = null;

            if (!string.IsNullOrEmpty(txtConfirmInvoiceDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtConfirmInvoiceDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtConfirmInvoiceDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtConfirmInvoiceDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ConfirmInvoiceDate = new Range<DateTime?>(dateStart, dateEnd);
            }

            decimal invoiceAmountStart, invoiceAmountEnd;

            if (decimal.TryParse(txtInvoiceAmountStart.Text, out invoiceAmountStart))
            {
                criteria.InvoiceAmountStart = invoiceAmountStart;
            }
            if (decimal.TryParse(txtInvoiceAmountEnd.Text, out invoiceAmountEnd))
            {
                criteria.InvoiceAmountEnd = invoiceAmountEnd;
            }
            if (!string.IsNullOrEmpty(txtInvoiceNo.Text))
            {
                criteria.InvoiceNO = txtInvoiceNo.Text.Trim();
            }



            return criteria;


        }

    }
}